import os
from configparser import ConfigParser


class DelLayout:
    def __init__(self):
        cfg = ConfigParser()
        cfg.read('del_layout.ini')
        cfg.sections()
        self.layout_dir = cfg.get('production', "layout_dir")
        self.module_dir = cfg.get('production', "module_dir")
        self.ignore_dir = cfg.get('production', "ignore_dir")
        self.layout_suffix = cfg.get('production', "layout_suffix")

    def all_file(self, dir, ignore, condition, array):
        if not os.path.isdir(dir):
            return
        files = os.listdir(dir)
        for file in files:
            file_abs = os.path.join(dir, file)

            if ignore(file_abs):
                print(file_abs)
            elif os.path.isdir(file_abs):
                self.all_file(file_abs, ignore, condition, array)
            else:
                append = condition(file_abs)
                if append[0]:
                    array.append(append[1])

    def ignore(self, dir):

        return dir == self.ignore_dir

    def layout_condition(self, file):
        append = file.endswith(self.layout_suffix)
        if append:
            return (True, file.split('/')[-1])
        else:
            return (False, None)

    def app_dir_condition(self, file):
        return (True, file)

    def go(self):
        # files =os.listdir(module_dir)
        all_layout_files = []
        self.all_file(self.layout_dir, self.ignore, self.layout_condition, all_layout_files)

        all_files = []
        self.all_file(self.module_dir, self.ignore, self.app_dir_condition, all_files)

        # for layout_file in all_layout_files:


if __name__ == '__main__':
    obj = DelLayout()
    obj.go()
